<template>
  <div class="education-container">
    <!-- 顶部分类 -->
    <div class="category-section">
      <el-tabs v-model="activeCategory" class="category-tabs">
        <el-tab-pane label="全部" name="all">
          <template #label>
            <el-icon><Grid /></el-icon>
            <span>全部</span>
          </template>
        </el-tab-pane>
        <el-tab-pane label="生长发育" name="growth">
          <template #label>
            <el-icon><TrendCharts /></el-icon>
            <span>生长发育</span>
          </template>
        </el-tab-pane>
        <el-tab-pane label="营养饮食" name="nutrition">
          <template #label>
            <el-icon><Food /></el-icon>
            <span>营养饮食</span>
          </template>
        </el-tab-pane>
        <el-tab-pane label="疾病预防" name="disease">
          <template #label>
            <el-icon><FirstAidKit /></el-icon>
            <span>疾病预防</span>
          </template>
        </el-tab-pane>
        <el-tab-pane label="心理健康" name="mental">
          <template #label>
            <el-icon><Star /></el-icon>
            <span>心理健康</span>
          </template>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!-- 文章列表 -->
    <div class="article-grid" v-loading="loading">
      <div v-for="article in displayArticles" :key="article.id" class="article-card" @click="viewArticle(article)">
        <el-image :src="article.cover" fit="cover" class="article-cover" />
        <div class="article-content">
          <el-tag size="small" :type="getTagType(article.category)">{{ getCategoryText(article.category) }}</el-tag>
          <h3>{{ article.title }}</h3>
          <p class="article-desc">{{ article.description }}</p>
          <div class="article-meta">
            <span class="date">
              <el-icon><Calendar /></el-icon>
              {{ formatDate(article.createTime) }}
            </span>
            <span class="views">
              <el-icon><View /></el-icon>
              {{ article.views }}
            </span>
          </div>
        </div>
      </div>
    </div>

    <!-- 文章详情对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="currentArticle?.title"
      width="800px"
      class="article-dialog"
    >
      <template v-if="currentArticle">
        <div class="dialog-content" v-loading="detailLoading">
          <div class="article-header">
            <el-image :src="currentArticle.cover" fit="cover" class="dialog-cover" />
            <div class="article-info">
              <el-tag size="small" :type="getTagType(currentArticle.category)">
                {{ getCategoryText(currentArticle.category) }}
              </el-tag>
              <div class="article-meta">
                <span class="date">发布于：{{ formatDate(currentArticle.createTime) }}</span>
                <span class="views">阅读：{{ currentArticle.views }}</span>
              </div>
            </div>
          </div>
          <div class="article-body rich-text" v-html="currentArticle.content"></div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { 
  Grid,
  TrendCharts,
  Food,
  FirstAidKit,
  Star,
  User,
  Calendar,
  View
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { getArticleList, getArticleDetail, increaseViews } from '@/api/article'

// 当前选中的分类
const activeCategory = ref('all')

// 文章列表数据
const articles = ref([])
const loading = ref(false)

// 获取文章列表
const fetchArticles = async () => {
  loading.value = true
  try {
    const params = {
      category: activeCategory.value
    }
    
    const res = await getArticleList(params)
    articles.value = res
  } catch (error) {
    console.error('获取文章列表失败:', error)
    ElMessage.error('获取文章列表失败')
  } finally {
    loading.value = false
  }
}

// 监听分类变化
watch(activeCategory, () => {
  fetchArticles()
})

// 根据分类筛选文章
const displayArticles = computed(() => articles.value)

// 获取标签类型
const getTagType = (category) => {
  const typeMap = {
    'growth': 'primary',
    'nutrition': 'success',
    'disease': 'warning',
    'mental': 'info'
  }
  return typeMap[category] || 'info'
}

// 获取分类文本
const getCategoryText = (category) => {
  const textMap = {
    'growth': '生长发育',
    'nutrition': '营养饮食',
    'disease': '疾病预防',
    'mental': '心理健康'
  }
  return textMap[category] || category
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '--'
  const date = new Date(dateString)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}

// 文章详情对话框
const dialogVisible = ref(false)
const currentArticle = ref(null)
const detailLoading = ref(false)

// 查看文章详情
const viewArticle = async (article) => {
  detailLoading.value = true
  currentArticle.value = article // 先显示列表中的基本信息
  dialogVisible.value = true
  
  try {
    // 获取完整的文章详情
    const detail = await getArticleDetail(article.id)
    if (detail) {
      currentArticle.value = detail
      // 增加阅读量
      await increaseViews(article.id)
      // 更新列表中的阅读量
      const index = articles.value.findIndex(item => item.id === article.id)
      if (index !== -1) {
        articles.value[index].views = detail.views
      }
    }
  } catch (error) {
    console.error('获取文章详情失败:', error)
    ElMessage.error('获取文章详情失败')
  } finally {
    detailLoading.value = false
  }
}

// 页面加载时获取文章列表
onMounted(() => {
  fetchArticles()
})
</script>

<style scoped>
.education-container {
  padding: 24px;
  background: #f5f7fa;
  min-height: 100vh;
}

.category-section {
  background: white;
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

:deep(.category-tabs .el-tabs__item) {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 20px;
}

:deep(.category-tabs .el-tabs__item .el-icon) {
  margin-right: 4px;
}

.article-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
}

.article-card {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  cursor: pointer;
}

.article-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.article-cover {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.article-content {
  padding: 20px;
}

.article-content h3 {
  margin: 12px 0;
  font-size: 18px;
  font-weight: 600;
  color: #333;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.article-desc {
  color: #666;
  font-size: 14px;
  line-height: 1.6;
  margin-bottom: 16px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.article-meta {
  display: flex;
  align-items: center;
  gap: 16px;
  color: #999;
  font-size: 13px;
}

.article-meta span {
  display: flex;
  align-items: center;
  gap: 4px;
}

.dialog-content {
  padding: 20px;
}

.article-header {
  margin-bottom: 24px;
}

.dialog-cover {
  width: 100%;
  height: 300px;
  object-fit: cover;
  border-radius: 12px;
  margin-bottom: 16px;
}

.article-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 16px;
}

.article-body {
  line-height: 1.8;
  color: #333;
}

.article-body h2 {
  font-size: 20px;
  margin: 24px 0 16px;
  color: #333;
}

.article-body p {
  margin: 16px 0;
  color: #666;
}

:deep(.el-dialog) {
  border-radius: 16px;
}

:deep(.el-dialog__header) {
  margin: 0;
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
}

:deep(.el-dialog__body) {
  padding: 0;
}

.rich-text {
  line-height: 1.8;
  color: #333;
}

.rich-text :deep(h1) {
  font-size: 28px;
  margin: 24px 0 16px;
  color: #1a1a1a;
}

.rich-text :deep(h2) {
  font-size: 24px;
  margin: 20px 0 14px;
  color: #262626;
}

.rich-text :deep(h3) {
  font-size: 20px;
  margin: 18px 0 12px;
  color: #333;
}

.rich-text :deep(p) {
  margin: 16px 0;
  color: #595959;
  font-size: 16px;
}

.rich-text :deep(img) {
  max-width: 100%;
  height: auto;
  margin: 16px 0;
  border-radius: 8px;
}

.rich-text :deep(ul),
.rich-text :deep(ol) {
  margin: 16px 0;
  padding-left: 24px;
}

.rich-text :deep(li) {
  margin: 8px 0;
  color: #595959;
}

.rich-text :deep(blockquote) {
  margin: 16px 0;
  padding: 16px 24px;
  background: #f8f9fa;
  border-left: 4px solid #409EFF;
  color: #666;
}

.rich-text :deep(code) {
  background: #f5f7fa;
  padding: 2px 6px;
  border-radius: 4px;
  color: #e6a23c;
  font-family: monospace;
}

.rich-text :deep(pre) {
  background: #282c34;
  padding: 16px;
  border-radius: 8px;
  overflow-x: auto;
}

.rich-text :deep(pre code) {
  background: none;
  color: #abb2bf;
}

.rich-text :deep(table) {
  width: 100%;
  border-collapse: collapse;
  margin: 16px 0;
}

.rich-text :deep(th),
.rich-text :deep(td) {
  border: 1px solid #ebeef5;
  padding: 12px;
  text-align: left;
}

.rich-text :deep(th) {
  background: #f5f7fa;
  font-weight: 600;
}

.rich-text :deep(a) {
  color: #409EFF;
  text-decoration: none;
  transition: color 0.3s;
}

.rich-text :deep(a:hover) {
  color: #66b1ff;
}
</style> 